<template>
  <el-dialog
    v-model="dialogVisible"
    class="app-dialog"
    :show-close="false"
    :close-on-click-modal="false"
    align-center
    append-to-body
    width="360"
  >
    <template #header>
      <div class="app-dialog-header">
        <span class="title">登录</span>
        <div class="close" @click="dialogVisible = false">
          <svgIcon name="close" />
        </div>
      </div>
    </template>
    <div class="dialog-body">
      <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto" status-icon>
        <el-form-item label="前缀" prop="perfix">
          <el-select v-model="ruleForm.perfix" placeholder="前缀">
            <el-option label="+86" value="+86" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="username">
          <el-input v-model="ruleForm.username" placeholder="手机号" />
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="ruleForm.code" placeholder="验证码" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <div class="app-button-info cancel" @click="dialogVisible = false">取消</div>
        <div class="app-button-primary confirm" @click="validateSubmit">登录</div>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElDialog } from "element-plus";
import { ref, computed, reactive } from "vue";
import { login } from "@/api/modules/login";
import { useUserStore } from "@/stores/modules/user.js";
const userStore = useUserStore();
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  title: {
    type: String,
    default: "确认"
  },
  content: {
    type: String,
    default: ""
  }
});
const emit = defineEmits(["update:modelValue", "confirm"]);
const dialogVisible = computed({
  get: () => {
    return props.modelValue;
  },
  set: val => {
    emit("update:modelValue", val);
  }
});

const ruleFormRef = ref(null);

const ruleForm = reactive({
  pwd: "",
  type: 1,
  username: "",
  code: "",
  perfix: "+86"
});

const rules = reactive({
  perfix: [{ required: true, message: "请选择", trigger: "change" }],
  username: [{ required: true, message: "请输入", trigger: "blur" }],
  code: [{ required: true, message: "请输入", trigger: "blur" }]
});
// 验证提交
async function validateSubmit() {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      submit();
    } else {
      console.log("error submit!", fields);
    }
  });
}
// 提交
async function submit() {
  const res = await login(ruleForm);
  if (res.code === 200) {
    userStore.loginVisible = false;
    // 设置token
    userStore.setToken(res.data.token);
    // 设置token
    userStore.setUserId(res.data.userId);
    userStore.getUserInfo();
  }
}
</script>

<style scoped lang="scss">
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 8px;
  .cancel {
    width: 148px;
  }
  .confirm {
    width: 148px;
  }
}
.dialog-body {
  padding-top: 30px;
  padding-bottom: 42px;
  font-size: 14px;
  font-weight: 400;
  line-height: 28px;
  color: #717681;
  text-align: center;
}
</style>
